------------------------------------------------------------------------------------------------------------------------------------------------
-- -- -- tasks base lib
-- local function language_change_say(theNPC)
local function language_change_say(theNPC)
    theNPC.CHAT_TABLE = {}
    pcall(function()
        -- theNPC.npc_base_lib:Get_Language() == "ch"
        theNPC.CHAT_TABLE = TUNING.NPC_CHAT_TABLE[ theNPC.npc_base_lib:Get_Language() ] ["npc_event_minifan"] or {}
    end)
end
-------------------------------------------------------------------------------------------------------------------------------------------------
local function talk_fn(theNPC,_table)
    if _table and _table.talk then
        theNPC.npc_base_lib:Say(_table.talk)
    end
end

local function Time_Out_Leave(inst)
    local theNPC = inst.entity:GetParent()
    local time = theNPC.npc_base_lib:Get_Time_Out()
    if time then
        inst:DoTaskInTime(time,function()
            inst:Remove()
            theNPC:AddDebuff("npc_event_leave","npc_event_leave")
            -- theNPC.npc_base_lib:Say(theNPC.CHAT_TABLE.time_out)
            print("Warning : npc time out and leave")
        end)
    end
end

local function CreateGift_pack(theNPC)
    local gift_1 = theNPC.npc_base_lib:Get_Base_Food_List()
    local gift_2 = theNPC.npc_base_lib:Get_Cooked_Food_List()
    local gift_3 = theNPC.npc_base_lib:Get_Equipments_Hand()
    local gift_4 = theNPC.npc_base_lib:Get_Equipments_Hats()
    local gift_5 = theNPC.npc_base_lib:Get_Equipments_Body()
    local gift_6 = theNPC.npc_base_lib:Get_Base_Materials()
    local gift_7 = theNPC.npc_base_lib:Get_Special_Materials()

    local Item_cmd = {{"log",1},{"goldnugget",1}}
    if TUNING.NPC_CONFIG.REWARDS_LESS then
        Item_cmd = {
            { theNPC.npc_base_lib:GetRandom(gift_7) , 0.2  },
            { theNPC.npc_base_lib:GetRandom(gift_5) , 0.2  },
            { "lightninggoathorn" , 1 },
            { "goatmilk" , 5 },
        }

    else
        Item_cmd = {
            { theNPC.npc_base_lib:GetRandom(gift_2) , 0.3  },
            { theNPC.npc_base_lib:GetRandom(gift_3) , 0.3  },
            { theNPC.npc_base_lib:GetRandom(gift_4) , 0.3  },
            { theNPC.npc_base_lib:GetRandom(gift_5) , 0.3  },
            { theNPC.npc_base_lib:GetRandom(gift_6) , 0.3  },
            { theNPC.npc_base_lib:GetRandom(gift_7) , 0.3  },
            { "lightninggoathorn" , 10 },
            { "goatmilk" , 20 },
        }
    end


    local gift_pack = SpawnPrefab("npc_gift_pack")
    gift_pack:PushEvent("ADD",{
        Name = theNPC.CHAT_TABLE.gift_name or "Gift",
        Skin = math.random(6),
        Items = Item_cmd,
        -- Items = {
        --         {"log",num},
        --         {"goldnugget",20}
        -- }

    })

    theNPC.components.inventory:GiveItem(gift_pack)
end

local function give_gift_pack(theNPC)
    theNPC.components.inventory:DropEverythingWithTag("npc_gift_pack")    
end

local function Give_minifan(theplayer)
    theplayer.components.inventory:GiveItem(SpawnPrefab("minifan"))
end

local function Check_minifan(theNPC,theplayer)
    local function Special_effects_in_doer(doer)
        -- SpawnPrefab("pocketwatch_heal_fx").Transform:SetPosition(theplayer.Transform:GetWorldPosition())
        -- SpawnPrefab("shadow_bishop_fx").Transform:SetPosition(theNPC.Transform:GetWorldPosition())
        local inst = SpawnPrefab("npc_fx_shadow_feather")
        
        if doer:HasTag("player") then

            SpawnPrefab("npc_fx_shadow_feather"):PushEvent("Set",{
                pt = Vector3(doer.Transform:GetWorldPosition()),
                color = Vector3(139,34,34),
                a = 0.1
            })
        else
            SpawnPrefab("npc_fx_shadow_feather"):PushEvent("Set",{
                pt = Vector3(doer.Transform:GetWorldPosition()),
                color = Vector3(255,255,255),
                a = 0.1
            })
        end

        -- inst.Transform:SetPosition(doer.Transform:GetWorldPosition())
    end

    -- local weapon = theplayer.components.combat:GetWeapon()
    local weapon =  theplayer.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
    if weapon and weapon.prefab == "minifan" then
        Special_effects_in_doer(theNPC)
        Special_effects_in_doer(theplayer)
        local ret = theNPC.npc_everything_data:Add("time",0.5)
        if ret % 4 == 0 then
            theNPC.npc_base_lib:Say(theNPC.CHAT_TABLE.hahaha)
        end
        if ret > 45 then
            return true
        end
    end
    return false
end
-------------------------------------------------------------------------------------------------------------------------------------------
local function NPC_TASKS_INIT(inst)
    local theNPC = inst.entity:GetParent()
    language_change_say(theNPC)
    Time_Out_Leave(inst)
    -- local  theplayer = theNPC.TheLinkPlayer
    -- theNPC.NPC_BASE_LIB:giveNpcHat(theNPC)
    theNPC.npc_base_lib:Equip_Random_Hat()
    theNPC.npc_base_lib:Equip_Random_Cloth()
    -- theNPC.npc_base_lib:Equip_Random_Weapon()
    theNPC.npc_base_lib:Equip(SpawnPrefab("minifan"))
    theNPC.npc_base_lib:Set_Speed_Mult(1.3)
    theNPC:AddTag("INLIMBO")
    theNPC:AddTag("notarget")
    theNPC:AddTag("noattack")
    theNPC:AddTag("invisible")
    theNPC.npc_event = inst.prefab

    CreateGift_pack(theNPC)


end

local function NPC_TASKS(inst)
    local theNPC = inst.entity:GetParent()
    if TheWorld.state.isnight  or TheWorld:HasTag("cave") then
        theNPC.npc_base_lib:Try_2_Set_Light(true)
    else
        theNPC.npc_base_lib:Try_2_Set_Light(false)
    end
    local theplayer = theNPC.__link_player
    if theplayer == nil or theplayer:IsValid() == false then
        -- theNPC:RemoveDebuff(inst.prefab)
        inst:Remove()
        theNPC:AddDebuff("npc_event_leave","npc_event_leave")
        print("Warning : theNPC.__link_player == nil")
        return
    end

    if theNPC.event_step == nil then

        if theNPC.npc_base_lib:Close_2_Target_In_Range({range = 3}) == true then
            theNPC.event_step = 1
        end
        
    elseif theNPC.event_step == 1 then

        theNPC.npc_base_lib:FaceToInst()
        theNPC.npc_base_lib:Action_Give()

        theNPC.event_step = theNPC.event_step + 1

    elseif theNPC.event_step == 2 then
        theNPC.npc_base_lib:FaceToInst()        
        Give_minifan(theplayer)
        theNPC.npc_base_lib:Say(theNPC.CHAT_TABLE.let_us_play_minifan)
        theNPC.event_step = 3

    elseif theNPC.event_step == 3 then

        theNPC.npc_base_lib:Run_Around_Target_In_Range({range = 5,random_range_flag = true ,mid_reflash_range = 1 })
        if Check_minifan(theNPC,theplayer) == true then
            theNPC.event_step = 4
        end
    elseif theNPC.event_step == 4 then
        theNPC.npc_base_lib:Say(theNPC.CHAT_TABLE.happy)
        theNPC.event_step = theNPC.event_step + 1 
    elseif theNPC.event_step == 5 then
        theNPC.event_step = theNPC.event_step + 1 
    elseif theNPC.event_step == 6 then        
        theNPC.event_step = theNPC.event_step + 1 
    elseif theNPC.event_step == 7 then        
        theNPC.event_step = theNPC.event_step + 1 
    elseif theNPC.event_step == 8 then        
        theNPC.event_step = theNPC.event_step + 1 
    elseif theNPC.event_step == 9 then        
        theNPC.event_step = theNPC.event_step + 1 
    elseif theNPC.event_step == 10 then
        theNPC.npc_base_lib:Say(theNPC.CHAT_TABLE.it_is_for_you)
        give_gift_pack(theNPC)
        
        theNPC:DoTaskInTime(2.5,function()
            theNPC.event_step = 20
        end)
        theNPC.event_step = theNPC.event_step + 1 

    elseif theNPC.event_step == 20 then
        -- theNPC.event_step = theNPC.event_step + 1
        -- theNPC:RemoveDebuff(inst.prefab)
        inst:Remove()
        theNPC.npc_base_lib:Say("Bye ~ ")
        theNPC:AddDebuff("npc_event_leave","npc_event_leave")
    end
end
--------------------------------------------------------------------------------------------------------------------------------------------------

local function OnAttached(inst,target) -- when the debuff on or player is newSpawn(or through the cave)
    inst.entity:SetParent(target.entity)
    inst.Network:SetClassifiedTarget(target)
    local NPC = inst.entity:GetParent()
    local flag ,error_code= pcall(function(inst,target)
        if TheWorld.ismastersim then

            NPC_TASKS_INIT(inst)


        end
    end,inst,target)
    if flag == false  then
        print("Error : OnAttached(inst,target) ",inst.prefab)
        print(error_code)
        NPC:Remove()
    end
end

local function OnDetached(inst) -- When eliminating from the outside
    local player = inst.entity:GetParent()   
    -- print(" FROM OUSIDE REMOVE ")
    
    -- inst:Remove()
end

local function ExtendDebuff(inst)
    -- inst.countdown = 3 + (inst._level:value() < CONTROL_LEVEL and EXTEND_TICKS or math.floor(TUNING.STALKER_MINDCONTROL_DURATION / FRAMES + .5))
end



local function OnUpdate(inst,ismastersim)
    local NPC = inst.entity:GetParent()
    local flag,error_code = pcall(function(inst,ismastersim)
        -- local NPC = inst.entity:GetParent()

        NPC_TASKS(inst)


    end,inst,ismastersim)
    if flag == false then
        print("Error : OnUpdate(inst,ismastersim) ,step:",NPC.event_step ,inst.prefab)
        print(error_code)
        NPC:Remove()
    end
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddNetwork()

    inst:AddTag("CLASSIFIED")


    -- inst:DoPeriodicTask(1, OnUpdate, nil, TheWorld.ismastersim)  -- in one seconds
    inst:DoPeriodicTask( 0.5, OnUpdate, nil, TheWorld.ismastersim)  -- in 0.5 seconds

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("debuff")
    inst.components.debuff:SetAttachedFn(OnAttached)
    inst.components.debuff.keepondespawn = false -- 是否保持debuff 到下次登陆
    -- inst.components.debuff:SetDetachedFn(inst.Remove)
    inst.components.debuff:SetDetachedFn(OnDetached)
    -- inst.components.debuff:SetExtendedFn(ExtendDebuff)
    -- ExtendDebuff(inst)


    return inst
end

return Prefab("npc_event_minifan", fn)
